package com.airpi.activities;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.airpi.R;
import com.airpi.heavy_tasks.AsyncTaskFragment;

public class MainRightFragment extends Fragment implements Observer{

	//Initialisation of the two List Views (User List and Media List)
	private ListView listMediaView;
	private ArrayAdapter<String> listViewAdapter;
	private ArrayList<String> mediaList;
	ArrayList<String> arr;


	public void setMediaList(String[] mdList){
		System.out.println("SET MEDIA LIST");
		String[] itemsMedia = mdList;
		ArrayList<String> items = new ArrayList<String>();

		if(itemsMedia != null){
			for(String item : itemsMedia){
				String[] it = item.split(";");
				if(it.length==3){
					items.add(it[0] + " (" + it[1] + ")" + ", " +it[2]);
				}
			}
		}
		FragmentActivity frag = getActivity();
		if(frag != null){
			System.out.println("Mise a jour MediaList");
			listViewAdapter = new ArrayAdapter<String>(frag, android.R.layout.simple_list_item_1, items);
			if(listMediaView!=null){
				listMediaView.setAdapter(listViewAdapter);
			}
		}
	}

	/**
	 * Method called on the generation of the main UI view, handling the different fragments
	 * On generation of the View, it inflates the layout of the different tabs
	 */

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView;
		rootView = inflater.inflate(R.layout.main_media_list, container, false);
		listMediaView = (ListView)rootView.findViewById(R.id.main_media_listview);
		if(listMediaView != null){
			listMediaView.setOnItemLongClickListener(new OnItemLongClickListener(){
				// setting onItemLongClickListener and passing the position to the function
				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
					removeItemFromList(position);   
					return true;
				}
			});
			listMediaView.setAdapter(this.listViewAdapter);
		}
		
		new Thread() {
			private AsyncTaskFragment atc;
			private Fragment f;

			public Thread set(Fragment f) {
				this.f = f;
				return this;
			}
			
			@Override
			public void run() {
				while(true) {
					try {
						atc = new AsyncTaskFragment(((Observer)f), "MediaList");
						atc.execute();
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.set(this).start();

	
		return rootView;
	}
	//method to remove list item
	protected void removeItemFromList(int position) {
		final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
		final int deletePosition = position;

		AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());

		alert.setTitle("Supprimer");
		alert.setMessage(R.string.delete_media);
		DialogInterface.OnClickListener ocl = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				AsyncTaskFragment atcDel = new AsyncTaskFragment(MainRightFragment.this, "DeleteSong:"+arr.get(deletePosition).
						split(";")[0]+";"+sharedPrefs.getString(LoginActivity.LOGIN_USER, null));
				atcDel.execute();
			}
		};

		DialogInterface.OnClickListener oclN = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		};

		alert.setPositiveButton(R.string.yes, ocl);
		alert.setNegativeButton(R.string.no, oclN);
		alert.show();

	}

	@Override
	public void notifier(String s) {
		System.out.println("result = " + s);
		if(s.contains("MediaList")){
			String [] res = s.substring(20).split("/");
			this.setMediaList(res);
		}
	}
}
